package com.example.jm24;

import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class Dialog extends AppCompatActivity {
    private Button btn;
    //private EditText realname,tel,timeinto;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);//安卓进入页面避免软键盘弹出的解决办法
        Bundle b=getIntent().getExtras();
        //获取Bundle的信息
        final String househost=b.getString("name");
        final String housename=b.getString("housename");
        System.out.println("获取名字："+housename);
        final String username=b.getString("username");
        //final String a=househost;
        //final String c=housename;
        //final String d=username;
        //realname=findViewById(R.id.realname);
        //tel=(EditText)findViewById(R.id.usertel);
        //timeinto=findViewById(R.id.time);
        //setContentView(R.layout.activity_introduce);
        /*btn=findViewById(R.id.btn1);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                AlertDialog.Builder builder= new AlertDialog.Builder(getApplicationContext());
                View view= LayoutInflater.from(getApplicationContext()).inflate(R.layout.dialog_into, null);
                TextView cancel =view.findViewById(R.id.choosepage_cancel);
                TextView sure =view.findViewById(R.id.choosepage_sure);
                final EditText realname =view.findViewById(R.id.realname);
                final EditText usertel =view.findViewById(R.id.usertel);
                final EditText time =view.findViewById(R.id.time);

                final AlertDialog dialog= builder.create();
//                dialog.show();
                dialog.getWindow().setContentView(view);
                //使editext可以唤起软键盘
                dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
                cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(getApplicationContext(), "cancel", Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                    }
                });
                sure.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(getApplicationContext(), "sure", Toast.LENGTH_SHORT).show();
                        btn.setText(realname.getText().toString()+ "");
                        dialog.dismiss();
                    }
                });
            }
        });*/

                AlertDialog.Builder builder= new AlertDialog.Builder(this);//getApplication不行
                View view= LayoutInflater.from(this).inflate(R.layout.dialog_into, null);
                TextView cancel =view.findViewById(R.id.choosepage_cancel);
                TextView sure =view.findViewById(R.id.choosepage_sure);
                final EditText realname =view.findViewById(R.id.realname);
                final EditText usertel =view.findViewById(R.id.usertel);
                final EditText time =view.findViewById(R.id.time);

                final AlertDialog dialog= builder.create();
                dialog.show();
                dialog.getWindow().setContentView(view);
                //使editext可以唤起软键盘
                dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
                cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(Dialog.this, "cancel", Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                    }
                });
                sure.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //Toast.makeText(Dialog.this, "预约成功", Toast.LENGTH_SHORT).show();
                        //btn.setText(realname.getText().toString()+ "");
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                String n = realname.getText().toString().trim();
                                String u = usertel.getText().toString().trim();
                                String t = time.getText().toString().trim();
                                if (n.equals("")||usertel.equals("")||t.equals("")){
                                    Looper.prepare();
                                    dialog.dismiss();
                                    Toast toast = Toast.makeText(getApplicationContext(),"输入不能为空！",Toast.LENGTH_SHORT);
                                    toast.show();
                                    Looper.loop();
                                }
                                UserDao  k= new UserDao();
                                final Data get=k.GetUser(getIntent().getStringExtra("username"));
                                UserDao ud = new UserDao();
                                Boolean res=ud.checkinsert(housename,username);
                                if (res) {
                                    Looper.prepare();
                                    Toast toast = Toast.makeText(getApplicationContext(), "已入住！", Toast.LENGTH_SHORT);
                                    toast.show();
                                    Looper.loop();
                                } else {
                                    int result = ud.yuyue(username, n, u, t, househost, housename,get.getUrl());
                                    if (result > 0) {
                                        Looper.prepare();
                                        Toast toast = Toast.makeText(getApplicationContext(), "预约成功", Toast.LENGTH_SHORT);
                                        toast.show();
                                        Looper.loop();
                                    }/*else{
                                    Looper.prepare();
                                    Toast toast=Toast.makeText(getApplicationContext(),"预约失败",Toast.LENGTH_SHORT);
                                    toast.show();

                                    //一下代码为跳转界面
                                    Intent intent=new Intent(getApplicationContext(),MainActivity.class);
                                    intent.putExtra("username",n);
                                    startActivity(intent);
                                    Looper.loop();

                                }*/

                                    //以上为jdbc登录
                                }
                            }
                        }).start();
                        dialog.dismiss();
                    }
                });
    }
}
//android:theme="@style/Theme.AppCompat.Dialog"
